//
//  CTLWaterflowLayout.h
//  瀑布流控件
//
//  Created by DWE_KJ on 2016/11/30.
//  Copyright © 2016年 DWE_KJ. All rights reserved.
//

#import "ViewController.h"
#import "CTLWaterflowLayout.h"
#import "MJExtension.h"
#import "MJRefresh.h"
#import "MMCommodityCell.h"
#import "MMCommodityModel.h"

@interface ViewController () <UICollectionViewDataSource, CTLWaterflowLayoutDelegate>
/** 所有的商品数据 */
@property (nonatomic, strong) NSMutableArray *listArray;

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) CTLWaterflowLayout *layout;
@end

@implementation ViewController

static NSString * const XMGShopId = @"shop";
static NSString * const isCell = @"isCell";


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupLayout];
    
    [self setupRefresh];
}

- (void)setupRefresh
{
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewShops)];
    [self.collectionView.mj_header beginRefreshing];
    
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreShops)];
    self.collectionView.mj_footer.hidden = YES;
}

- (void)loadNewShops
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSArray *shops = [MMCommodityModel mj_objectArrayWithFilename:@"1.plist"];
        [self.listArray removeAllObjects];
        [self.listArray addObjectsFromArray:shops];
        
        // 刷新数据
        [self.collectionView reloadData];
        
        [self.collectionView.mj_header endRefreshing];
    });
}

- (void)loadMoreShops
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSArray *shops = [MMCommodityModel mj_objectArrayWithFilename:@"1.plist"];
        [self.listArray addObjectsFromArray:shops];
        
        // 刷新数据
        [self.collectionView reloadData];
        
        [self.collectionView.mj_footer endRefreshing];
    });
}

- (void)setupLayout
{    
    // 注册
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([MMCommodityCell class]) bundle:nil] forCellWithReuseIdentifier:isCell];
}

#pragma mark - <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    self.collectionView.mj_footer.hidden = self.listArray.count == 0;
    return self.listArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MMCommodityCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:isCell forIndexPath:indexPath];
    cell.commodityModel = self.listArray[indexPath.item];
    return cell;
}

#pragma mark - <XMGWaterflowLayoutDelegate>
- (CGFloat)waterflowLayout:(CTLWaterflowLayout *)waterflowLayout heightForItemAtIndex:(NSUInteger)index itemWidth:(CGFloat)itemWidth
{
    MMCommodityModel *shop = self.listArray[index];
    
    return itemWidth * shop.h / shop.w;
}

- (CGFloat)rowMarginInWaterflowLayout:(CTLWaterflowLayout *)waterflowLayout
{
    return 20;
}

- (CGFloat)columnCountInWaterflowLayout:(CTLWaterflowLayout *)waterflowLayout
{
    if (self.listArray.count <= 50) return 2;
    return 3;
}

- (UIEdgeInsets)edgeInsetsInWaterflowLayout:(CTLWaterflowLayout *)waterflowLayout
{
    return UIEdgeInsetsMake(10, 20, 30, 10);
}

#pragma mark -- GET
- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        // 创建布局
        _layout = [[CTLWaterflowLayout alloc] init];
        _layout.delegate = self;
        
        // 创建CollectionView
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:_layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.dataSource = self;
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}

- (NSMutableArray *)listArray
{
    if (!_listArray)
    {
        _listArray = [NSMutableArray array];
    }
    return _listArray;
}



@end
